<script>
import { GlIcon, GlPopover, GlSkeletonLoader, GlTooltipDirective } from '@gitlab/ui';
import query from 'ee_else_ce/issuable/popover/queries/issue.query.graphql';
import IssueDueDate from '~/boards/components/issue_due_date.vue';
import IssueMilestone from '~/issuable/components/issue_milestone.vue';
import StatusBox from '~/issuable/components/status_box.vue';
import { STATUS_CLOSED } from '~/issues/constants';
import timeagoMixin from '~/vue_shared/mixins/timeago';
import WorkItemTypeIcon from '~/work_items/components/work_item_type_icon.vue';

export default {
  components: {
    GlIcon,
    GlPopover,
    GlSkeletonLoader,
    IssueDueDate,
    IssueMilestone,
    IssueWeight: () => import('ee_component/boards/components/issue_card_weight.vue'),
    StatusBox,
    WorkItemTypeIcon,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  mixins: [timeagoMixin],
  props: {
    target: {
      type: HTMLAnchorElement,
      required: true,
    },
    namespacePath: {
      type: String,
      required: true,
    },
    iid: {
      type: String,
      required: true,
    },
    cachedTitle: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      issue: {},
    };
  },
  computed: {
    formattedTime() {
      return this.timeFormatted(this.issue.createdAt);
    },
    title() {
      return this.issue?.title || this.cachedTitle;
    },
    showDetails() {
      return Object.keys(this.issue).length > 0;
    },
    isIssueClosed() {
      return this.issue?.state === STATUS_CLOSED;
    },
  },
  apollo: {
    issue: {
      query,
      update: (data) => data.project.issue,
      variables() {
        const { namespacePath, iid } = this;

        return {
          projectPath: namespacePath,
          iid,
        };
      },
    },
  },
};
</script>

<template>
  <gl-popover :target="target" boundary="viewport" placement="top" show>
    <gl-skeleton-loader v-if="$apollo.queries.issue.loading" :height="15">
      <rect width="250" height="15" rx="4" />
    </gl-skeleton-loader>
    <div v-else-if="showDetails" class="gl-display-flex gl-align-items-center">
      <status-box issuable-type="issue" :initial-state="issue.state" />
      <gl-icon
        v-if="issue.confidential"
        v-gl-tooltip
        name="eye-slash"
        :title="__('Confidential')"
        class="gl-text-orange-500 gl-mr-2"
        :aria-label="__('Confidential')"
      />
      <span class="gl-text-secondary">
        {{ __('Opened') }} <time :datetime="issue.createdAt">{{ formattedTime }}</time>
      </span>
    </div>
    <h5 v-if="!$apollo.queries.issue.loading" class="gl-my-3">{{ title }}</h5>
    <!-- eslint-disable @gitlab/vue-require-i18n-strings -->
    <div>
      <work-item-type-icon v-if="!$apollo.queries.issue.loading" :work-item-type="issue.type" />
      <span class="gl-text-secondary">{{ `${namespacePath}#${iid}` }}</span>
    </div>
    <!-- eslint-enable @gitlab/vue-require-i18n-strings -->

    <div v-if="!$apollo.queries.issue.loading" class="gl-display-flex gl-text-secondary gl-mt-2">
      <issue-due-date
        v-if="issue.dueDate"
        :date="issue.dueDate.toString()"
        :closed="isIssueClosed"
        tooltip-placement="top"
        class="gl-mr-4"
        css-class="gl-display-flex gl-white-space-nowrap"
      />
      <issue-weight
        v-if="issue.weight"
        :weight="issue.weight"
        tag-name="span"
        class="gl-display-flex gl-mr-4"
      />
      <issue-milestone
        v-if="issue.milestone"
        :milestone="issue.milestone"
        class="gl-display-flex gl-overflow-hidden"
      />
    </div>
  </gl-popover>
</template>
